#include "net/TCPServer.h"
#include "net/TCPConnection.h"
#include "eventloop/Eventloop.h"
#include "net/Sockaddr.h"

#include <stdio.h>
#include <unistd.h>

void onConnection(const TCPConnectionPtr& conn)
{
  if (conn->is_connected())
  {
    printf("onConnection(): new connection [%s] from %s\n",
           conn->name().c_str(),
           conn->peer().IP_port().c_str());
  }
  else
  {
    printf("onConnection(): connection [%s] is down\n",
           conn->name().c_str());
  }
}

void onMessage(const TCPConnectionPtr& conn,
               const char* data,
               ssize_t len)
{
  printf("onMessage(): received %zd bytes from connection [%s], content: %s\n",
         len, conn->name().c_str(), data);
}

int main()
{
  printf("main(): pid = %d\n", getpid());

  Sockaddr listenAddr(8888);
  Eventloop loop;

  TCPServer server(loop, listenAddr, "TestServer");
  server.set_connection_callback(onConnection);
  server.set_message_callback(onMessage);
  server.start();

  loop.loop();
}
